'use strict';
//define(['common','angular','services'], function(common,angular) {
	angular.module('backcenter.roleList', [])

	.controller('roleListCtrl',['$rootScope', '$scope', '$uibModal', 'BizData', '$ocLazyLoad'
	                             , f])
    function f($rootScope,$scope, $uibModal, BizData, $ocLazyLoad) {

		var Model = this;
		

		/**
		name:bizData#onBeforeDelete
		description: <b>[回调型事件]</b>业务数据删除前
		@param {object} event 它的结构如下:<br/>
		{
			"source":组件的js对象
			,"cancel":可修改	，设置为true后中断当前delete动作
			,"deleteIDList":删除的行Id数组
		}
		*/
		Model.dataRoleBeforeDelete = function(event) {
//			alert(event.deleteIDList[0]);
		}
		/**
		name:bizData#onAfterDelete
		description: <b>[回调型事件]</b>业务数据删除后
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"deleteIDList" : 删除的行Id数组
		}
		</xmp>	
		*/
		Model.dataRoleAfterDelete = function(event) {
//			alert('删除之后！')
		}



		
		
		/**
		name:bizData#onIndexChanged
		description: <b>[回调型事件]</b>行记录变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"rowID" : 行Id,
			"rowIndex" : 行索引
		}
		</xmp>	
		*/
		Model.dataRoleIndexChanged = function(event) {
			
		}
		
		/**
		name:bizData#onDataChanged
		description: <b>[回调型事件]</b>数据发生变化（包括新增、刷新、删除、修改引起的变化），感知从data的变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"changedSource" ：触发变化的组件的js对象，当从data触发时返回从data对象,
			"selfChanged" : 是否自身变化，false：从data触发的变化,
			"type" : 触发变化的类型，包括：new、delete、refresh、valueChanged、clear，其他参数根据触发类型不同给出不同参数，参考onAfterNew等事件参数
		}
		</xmp>	
		*/
		Model.dataRoleDataChanged = function(event) {
			
		}
		
		/**
		name:bizData#onValueChanged
		description: <b>[回调型事件]</b>数据变化
		@param {object} event 
		<br/><b>结构如下：</b>
		<xmp> 
		{
			"source" : 组件的js对象,
			"column" : 关系,
			"rowIndex" : 行索引,
			"value" : 新值,
			"originalValue" : 旧值
		}
		</xmp>	
		*/
		//输入值变化，监视input的输入，ng-change
		Model.dataRoleValueChanged = function(event) {

		}
		


		/**
		name:bizData#onBeforeNew
		description: <b>[回调型事件]</b>业务数据新增前
		@param {object} event 它的结构如下:<br/>{"source":组件的js对象,"cancel":可修改，设置为true后中断当前new动作}
		 */
		Model.dataRoleBeforeNew = function(event) {

		};
		/**
		 * name:bizData#onAfterNew description: <b>[回调型事件]</b>业务数据新增后
		 * 
		 * @param event
		 *            它的结构如下:<br/>{"source":组件的js对象,"id":新增的行Id}
		 */
		Model.dataRoleAfterNew = function(event){
			
		}


		var options = {};
		options.container = Model;
		options.concept = 'SaOprole';
		options.name = 'dataRole';
		options.url = $rootScope.path;
		options.httpUrl = $rootScope.getServiceName();
		options.creater = '/backcenter/role/list/create';
		options.reader = '/backcenter/role/list/query';
		options.writer = '/backcenter/role/list/save';
		options.deleter = '/backcenter/role/list/delete';
		options.idColumnDefine = "sID";
		options.idColumnName = "sID";
		options.isTree = false;
		options.parentRelation = 'sParent';
		options.nodeKindRelation = 'sNodeKind';
		options.nodeLevelRelation = 'sLevel';
		options.pathRelation = 'sFID';
		options.directDeleteMode = true;
		options.limit = 10;
		options.orderBy = "SaOprole.sCode ASC";
		
		options.onIndexChanging = null;
		options.onIndexChanged = Model.dataRoleIndexChanged;
		options.onBeforeNew = Model.dataRoleBeforeNew;
		options.onAfterNew = Model.dataRoleAfterNew;
		options.onBeforeDelete = Model.dataRoleBeforeDelete;
		options.onAfterDelete = Model.dataRoleAfterDelete;
		options.onValueChanged = Model.dataRoleValueChanged;
		options.onDataChanged = Model.dataRoleDataChanged;
		
		$scope.dataRole = new BizData(options);

		//监视对象中的所有变动
		$scope.dataRole.deregRows = $scope.$watch('dataRole.rows',$scope.dataRole.watchRows,true);//true代表深度监视，包括对象的属性，而不是简单的一个值
		//////////////////////////////////////////////
		
		
		////////////////////启动执行开始///////////////////////////////////////////////////

    	
    	if($rootScope.checkTopMyOgn()==true){
    		
    		//设置过滤条件
    		$scope.dataRole.filter = "SaOprole.orgID = '"+ $rootScope.topMyOgn.sID +"'";//+" AND sOrgKindID='ogn'";

    		//刷新数据
    	    $scope.dataRole.refreshData();
    	}


		////////////////////启动执行结束///////////////////////////////////////////////////
			
		

		Model.grid1RowClick = function(event){
			
		}

		Model.grid1RowDblClick = function(event){
			Model.edit();
		}

		//添加(调用对话框)
		Model.add = function() {
			//打开对话框
			var options = {};
			
			options.recordState = 'new';
			options.id = $rootScope.topMyOgn.sID;//当前组织ID
			
	        var modalInstance = $uibModal.open({  
	        	backdrop: 'static',
	        	size: '',
	            templateUrl: $rootScope.getProjectName() + '/backcenter/role/single',  
	            controller: 'roleSingleCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(options);  
	                }
                    ,title : function() { return '新建'; }
                    ,loadCtrl : $ocLazyLoad.load('js/role/single')
		        }
	        });  
	        //模态窗口打开之后执行的函数
	        modalInstance.opened.then(function(){  
	        });  
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (result) {  
	             //成功关闭窗口
	             $scope.dataRole.refreshData();
	        }, function (reason) {  
	        	
	        });  
		};

		//修改
		Model.edit = function() {
			//打开对话框
			var index = $scope.dataRole.currentIndex;
			var row = $scope.dataRole.rows[index];

			var options = {};
			
			options.recordState = 'edit';
			options.id = row.sID.value;

	        var modalInstance = $uibModal.open({  
	        	backdrop: 'static',
	        	size: '',
	            templateUrl: $rootScope.getProjectName() + '/backcenter/role/single',  
	            controller: 'roleSingleCtrl as Model',  
	            resolve: {
	                params: function () {  
	                    return angular.copy(options);  
	                }
                    ,title : function() { return '修改'; }
                    ,loadCtrl : $ocLazyLoad.load('js/role/single')
		        }
	        });  
	        
	        //模态窗口打开之后执行的事件
	        modalInstance.opened.then(function(){
	        });  
	        
	        //模态窗口关闭之后执行的事件
	        modalInstance.result.then(function (result) {  
	             //成功关闭窗口
	             $scope.dataRole.refreshData();
//	             $scope.dataRole.currentIndex = index;
	        }, function (reason) {  
	        	
	        });  
				
		};

		//（删除角色）
		Model.remove = function(){
			var ids = new Array();
			for(var index=0;index<$scope.dataRole.rows.length;index++){
				if($scope.dataRole.rows[index].userdata.checked){
					ids.push($scope.dataRole.rows[index].sID.value);
				}
			}
			if(ids.length<=0){
            	$rootScope.addAlert('info','请选择需要删除的行。');
            	return;
			}
			$scope.dataRole.deleteData(ids);
			$scope.dataRole.refreshData();
		}
		


	    //表格标题中的选择框ng-model
	    $scope.checkboxRoleValue = false;
	    //表格标题中的选择对话框click事件
	    Model.checkboxRoleClick = function () {  
			for(var index=0;index<$scope.dataRole.rows.length;index++){
				$scope.dataRole.rows[index].userdata.checked=$scope.checkboxRoleValue;
			}
	    };    

	    ///////////////////////////页面级////////////////////////////////////////
		//返回
		Model.goBack = function(event) {
			window.history.go(-1);
		}
	    ///////////////////////////页面级////////////////////////////////////////

		return Model;
		
	}	
	
//});
